var allStackPoint = null;
var mold_type = null; //类别（设备/材料）
var miarl_type = null; //材料类型(变压器、联办、开关柜)
var time = null;
var gDName = null;
$(function () {
    $.when(
        getdatadictionary("mold_type"),
        $.get(baseURL+'csg/carrivalrecord/select?guid='+GetUrlParam("guid"),function (r) {
            allStackPoint = r.returnlist;
            vm.allStackPoint = allStackPoint;
        })/*,
		 $.get(baseURL+'csg/cgoodsdetail/select',function (r) {
		 miarl_type = r.returnlist;
		 vm.miarl_type = miarl_type;
		 })*/

    ).done(
        $("#jqGrid").jqGrid({
            url: baseURL + 'csg/cgoodsdetail/list?guid='+GetUrlParam("guid")+'&pGuid='+GetUrlParam("pGuid"),
            datatype: "json",
            colModel: [
                { label: 'guid', name: 'guid', index: 'guid', width: 50, key: true,hidden:true },
                { label: '所属堆放点', name: 'aRGuid', index: 'a_r_guid', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in allStackPoint) {
                            if (cellvalue == allStackPoint[d].guid) return allStackPoint[d].sPName;
                        }
                    }
                },
                { label: '材料中类', name: 'gDMidtype', index: 'g_d_midtype', width: 80 },
                { label: '材料名称', name: 'gDName', index: 'g_d_name', width: 80 },
                { label: '主要技术参数', name: 'gDParameter', index: 'g_d_parameter', width: 80 },
                { label: '到货数', name: 'gDNum', index: 'g_d_num', width: 80 },
                { label: '单位', name: 'gDUnit', index: 'g_d_unit', width: 80 },
                { label: '供货商', name: 'gDBusine', index: 'g_d_busine', width: 80 },
                { label: '类别(材料/设备)', name: 'gDType', index: 'g_d_type', width: 80,hidden:true },
                { label: '所属设备/材料ID', name: 'mDGuid', index: 'm_d_guid', width: 80,hidden:true },
                { label: '物质编码', name: 'gDCode', index: 'g_d_code', width: 80,hidden:true },
                { label: '材料大类', name: 'gDBigtype', index: 'g_d_bigtype', width: 80,hidden:true },
                { label: '材料标签', name: 'gDLabel', index: 'g_d_label', width: 80,hidden:true },
                { label: '购置总价', name: 'gDAllprice', index: 'g_d_allprice', width: 80,hidden:true },
                { label: '购置单价', name: 'gDPrice', index: 'g_d_price', width: 80,hidden:true },
                { label: '单重（kg）', name: 'gDWeight', index: 'g_d_weight', width: 80,hidden:true },
                { label: '状态', name: 'gDDatastate', index: 'g_d_datastate', width: 80,hidden:true }
            ],
            viewrecords: true,
            height: 385,
            rowNum: 10,
            rowList : [10,30,50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth:true,
            multiselect: true,
            pager: "#jqGridPager",
            jsonReader : {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames : {
                page:"page",
                rows:"limit",
                order: "order"
            },
            gridComplete:function(){
                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            }
        })
    )
});

//设备/材料


function getdatadictionary(sss)
{

    $.get(baseURL + "api/bdatadictionary/select?d_code="+sss, function(r){
        if(sss=="mold_type")
        {
            mold_type=r.returnlist;
        }

        vm.mold_type=mold_type;
    });
}





var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        time:time,
        gDName:gDName,
        mold_type:mold_type,
        miarl_type:miarl_type,
        cGoodsDetail: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.cGoodsDetail = {};
        },
        update: function (event) {
            var guid = getSelectedRow();
            if(guid == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(guid)
        },
        saveOrUpdate: function (event) {
            var guid = GetUrlParam("guid");
            var pGuid = GetUrlParam("pGuid");
            var url = vm.cGoodsDetail.guid == null ? "csg/cgoodsdetail/saveNew/"+guid+"/"+pGuid : "csg/cgoodsdetail/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.cGoodsDetail),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if(guids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "csg/cgoodsdetail/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(guid){
            $.get(baseURL + "csg/cgoodsdetail/info/"+guid, function(r){
                vm.cGoodsDetail = r.cGoodsDetail;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    gDName:vm.gDName,
                    time:vm.time
                },
                page:page
            }).trigger("reloadGrid");
        }
    }
});